package com.ykt.model;

import com.jfinal.plugin.activerecord.Page;

import java.util.List;
import java.util.Map;

/**
 * 管理员
 * @author long.hua
 * @version 1.0.0
 * @since 2016-01-14 23:14
 */
public class Admin extends BaseModel<Admin> {
    private static final long serialVersionUID = 1L;

    public static final String password = "password";
    public static final String phone = "phone";
    public static final String email = "email";
    public static final String status = "status";
    public static final String role_ids = "role_ids";
    
    public static final String shopId = "shop_id";
    public static final String community_id = "community_id";

    public static final Admin dao = new Admin();


    public List<String> getRolesName(String loginName) {
        String sql = "SELECT r.name FROM admin u "
                + "INNER JOIN admin_role ar ON u.id = ar.id "
                + "INNER JOIN role r ON r.id=ar.roleid "
                + "WHERE u.name=?";
        return getByField(sql, "name", loginName);
    }

    public Admin checkUser(String name, String pwd) {
        String sql = "SELECT * FROM admin WHERE name =? AND password=? ";
        return findFirst(sql, name, pwd);
    }

    //客户端进入前，员工先登录
    public Admin checkUserByEmployee(String name, String pwd, int shopId) {
        String sql = "SELECT * FROM admin WHERE name =? AND password=? AND shop_id=? ";
        return findFirst(sql, name, pwd, shopId);
    }

    public Role getRole() {
        return Role.dao.findById(this.get("role_id"));
    }

    @Override
    public Page<Admin> pageFindModel(int pageNumber, int rows, Map<String, String> paramMap) {
        return paginateHasShop(pageNumber, rows, paramMap);
    }

    /**
     * 关联 role的查询
     *
     * @param pageNumber
     * @param rows
     * @param paramMap
     * @return
     */
    public Page<Admin> paginateHasShop(int pageNumber, int rows, Map<String, String> paramMap) {
        String tableName = getTableName();
        String select = "SELECT a.*,r.name roleName";
        String sqlExceptSelect = " FROM " + tableName + " a "
        		+ " LEFT JOIN admin_role ar ON a.id=ar.admin_id"
        		+ " LEFT JOIN role r ON ar.role_id=r.id ";
        return pageFindModel(select, sqlExceptSelect, pageNumber, rows, paramMap);
    }

    /**
     * 获取主要的菜单分组
     *
     * @return
     */
    public List<Menu> getMenu() {
    	String sql = "SELECT DISTINCT ppm.* "
                + "FROM role r "
                + "INNER JOIN role_menu rm ON r.id = rm.role_id "
                + "INNER JOIN menu m ON rm.menu_id = m.id "
                + "INNER JOIN admin_role ar ON ar.role_id = r.id "
                + "INNER JOIN admin u ON ar.admin_id = u.id "
                + "INNER JOIN menu pm ON m.pid = pm.id "
                + "INNER JOIN menu ppm ON pm.pid = ppm.id "
                + "WHERE u.id = ? "
                + "ORDER BY ppm.seq";
        return Menu.dao.find(sql, get("id"));
    }

    /**
     * 根据PID查询所以子menu
     *
     * @param pid
     * @return
     */
    public List<Menu> getMenuSub(Integer pid) {
        String sql = "SELECT DISTINCT pm.* "
                + "FROM role r "
                + "INNER JOIN role_menu rm on r.id = rm.role_id "
                + "INNER JOIN menu m on rm.menu_id = m.id "
                + "INNER JOIN admin_role ar on ar.role_id = r.id "
                + "INNER JOIN admin u on ar.admin_id = u.id "
                + "INNER JOIN menu pm on m.pid = pm.id "
                + "INNER JOIN menu ppm on pm.pid = ppm.id "
                + "WHERE u.id = ? and ppm.id=? "
                + "ORDER BY pm.seq";
        return Menu.dao.find(sql, get("id"), pid);
    }

    /**
     * 得到所有操作类型的menu
     * 缓存 authority menuCommon_get("id")
     *
     * @return
     */
    public List<Menu> getMenuCommon() {
        String sql = "SELECT DISTINCT m.*  "
                + "FROM menu m "
                + "INNER JOIN role_menu rm ON rm.menu_id = m.id "
                + "INNER JOIN role r ON rm.role_id = r.id "
                + "INNER JOIN admin_role ar ON ar.role_id=r.id "
                + "WHERE ar.admin_id=? AND m.type=1";

        return Menu.dao.find(sql, get("id"));
    }

}
